package com.zzl.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.zzl.springboot.entity.Admin;
import com.zzl.springboot.entity.Role;
import com.zzl.springboot.entity.User;
import com.zzl.springboot.entity.UserInfo;
import com.zzl.springboot.service.AdminService;
import com.zzl.springboot.service.UserService;
import com.zzl.springboot.utils.DataResult;
import com.zzl.springboot.utils.UpDownUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @program: AAAzzl
 * @description:
 * @author: zzl
 * @create: 2022-11-18 20:39
 * @version:1.0
 **/
// jhasdhjka

@Api(tags = "管理员模块")
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private UserService userService;

    @RequestMapping("/index")
    public String toIndex(){
        return "admin/index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(HttpSession session){
        // fixme : session里面有用户登录数据则不能返回登录
        Object adminInfo = session.getAttribute("adminInfo");
        if (adminInfo!=null){
            return "admin/index";
        }
        return "admin/sign-in";
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "admin/sign-up";
    }

    @RequestMapping("/toList")
    public String toList(){
        return "admin/list";
    }

    @RequestMapping("/toTest")
    public String toTest(){
        return "admin/test";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "admin/add";
    }


    @ApiImplicitParam(name= "admin",value = "登录数据封装")
    @ApiOperation("管理员登录")
    @PostMapping("/login")
    @ResponseBody
    public DataResult login(@RequestBody Admin admin, HttpSession session){
        Admin adminInfo = adminService.getByUsername(admin.getUsername());
        if (adminInfo == null || !admin.getPassword().equals(adminInfo.getPassword())){
            System.out.println("账号密码戳五");
            return DataResult.fail("账号或密码错误");
        }
        Integer vol1 = userService.getVol1();
        Integer vol2 = userService.getVol2();
        Integer vol3 = userService.getVol3();
        Integer volNum = userService.getVolNum();
        System.out.println(vol1+vol2+vol3+"   "+volNum);
        session.setAttribute("vol1",vol1);
        session.setAttribute("vol2",vol2);
        session.setAttribute("vol3",vol3);
        session.setAttribute("volNum",volNum);
        session.setAttribute("adminInfo",adminInfo);
        return DataResult.success(adminInfo,null);
    }

    @ApiOperation("获取用户数据列表")
    @RequestMapping("/getUserList")
    @ResponseBody
    public DataResult getUserList(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "3") int pageSize,
                                  @RequestParam(defaultValue = "0") int roleId,
                                  @RequestParam(defaultValue = "") String searchInfo){
        PageInfo<User> page = null;
        if (roleId==0 && searchInfo.equals("")){
            page = userService.getUserList(pageNum, pageSize);
        }else if (roleId!=0 && searchInfo.equals("")){
            page =userService.getListByRoleId(pageNum,pageSize,roleId);
        }else {
            page = userService.getUserListBySearch(pageNum,pageSize,roleId,searchInfo);
        }

        return DataResult.success(page,null);
    }

    @ApiOperation("根据userId获取用户info信息")
    @RequestMapping("/getInfoByUserId")
    @ResponseBody
    public DataResult getInfoByUserId(Integer id){
        // fixme : 未对空值情况进行处理
        UserInfo userInfo = userService.getInfoByUserId(id);
        return DataResult.success(userInfo,null);
    }

    @ApiOperation("获取用户类别集合")
    @RequestMapping("/getRoleList")
    @ResponseBody
    public DataResult getRoleList(){
        List<Role> roleList = userService.getRoleList();
        System.out.println(roleList);
        return DataResult.success(roleList,null);
    }


    @ApiOperation("根据id获取用户数据")
    @RequestMapping("/getUserById")
    @ResponseBody
    public DataResult getUserById(Integer id){
        User user = userService.getUserById(id);
        System.out.println(user);
        return DataResult.success(user,null);
    }

    @ApiOperation("修改用户信息（只能修改密码、角色、状态）")
    @PostMapping("/updateUser")
    @ResponseBody
    // fixme: 如果接收User对象时使用@RequestParam会报错
    public DataResult updateUser(@RequestBody User user){
        System.out.println(user);
        Integer integer = userService.updateUser(user);
        System.out.println(integer);
        return DataResult.success(integer,null);
    }


    @ApiOperation("根据删除用户信息")
    @PostMapping("/delUser")
    @ResponseBody
    public DataResult delUser(Integer id){
        System.out.println(id+"  删除 ");
        Integer integer = userService.delById(id);
        if (integer==0){
            return DataResult.fail("该用户不存在");
        }
        return DataResult.success(integer,null);
    }


    @ApiOperation("批量删除")
    @PostMapping("/delChecked")
    @ResponseBody
    public DataResult delChecked(@RequestBody String[] ids){
        System.out.println(Arrays.toString(ids));
        Integer i = userService.delByChecked(ids);
        if (i==0){
            return DataResult.fail("目标数据不存在");
        }
        return DataResult.success(i,null);
    }


    @ApiOperation("添加志愿者")
    @PostMapping("/addUser")
    @ResponseBody
    public DataResult addUer(User user,
                             @RequestParam("headPic") MultipartFile headPic,
                             HttpServletRequest req){
        System.out.println("=============上传================");
//        System.out.println(username);
//        System.out.println(password);
//        System.out.println(user.toString());
        System.out.println(headPic);
        String pic = UpDownUtil.upload(req,headPic);
        System.out.println(pic);
        user.setUserImg(pic);
        user.setCreateTime(new Date());
        System.out.println(user.toString());
        Integer i = userService.addUser(user);
        return DataResult.success(i,null);
    }





    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.setAttribute("adminInfo",null);
        return "redirect:/admin/toLogin";
    }

}
